﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Web.UI;
using System.Threading;

using TimKen.DataEntity;
using TimKen.DataAccess;
using TimKen.BussLogic;

namespace TimKen.App
{
    /// <summary>
    /// 所以用户控件的基类
    /// </summary>
    public class BaseUserControl : UserControl
    {
        public string GetLangString(string key)
        {
            return TimKen.Localization.LocalizationUtility.GetText(key);
        }

        public int LanguageID
        {
            get
            {
                switch (Thread.CurrentThread.CurrentUICulture.ToString())
                {
                    case "zh-CN": return 1;
                    case "en-US": return 2;
                    default: return 1;
                }
            }
        }



        public string LanguageWhereStr
        {
            get
            {
                return " LanguageID=" + LanguageID;
            }
        }

        /// <summary>
        /// 上传文件相对路径
        /// </summary>
        public string UploadFilePath
        {
            get
            {
                return "/upload/";
            }
        }
        /// <summary>
        /// 上传文件物理路径
        /// </summary>
        public string PhysicalUploadFilePath
        {
            get
            {
                return Server.MapPath(UploadFilePath);
            }
        }

        /// <summary>
        /// 获取Bool值的图片
        /// </summary>
        /// <returns></returns>
        public string GetBoolPic(bool IsTrue)
        {
            return IsTrue ? "~/Admin/Images/yes.gif" : "~/Admin/Images/no.gif";
        }




        public string GetOptionType(string fieldType)
        {
            switch (fieldType)
            {
                case "0": return GetLangString("FieldType_TextBox");
                case "1": return GetLangString("FieldType_TextArea");
                case "2": return GetLangString("FieldType_Select");
                default: return GetLangString("FieldType_TextBox");
            }
        }

        /// <summary>
        /// 根据品牌ID获取品牌名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetBrandName(object id)
        {
            if (!TypeParse.IsNumeric(id))
                return "";

            int brandId = Convert.ToInt32(id);

            string sql = "SELECT  Name FROM  PBrand WHERE BrandID=" + brandId;
            object brandName = DbHelperSQL.GetSingle(sql);

            if (brandName == null)
                return "";

            return brandName.ToString();
        }



        /// <summary>
        /// 根据产品类型ID获取产品类型名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetCategoryName(object id)
        {
            if (!TypeParse.IsNumeric(id))
                return "";

            int categoryId = Convert.ToInt32(id);
            PCategoryManager categoryManager = new PCategoryManager();


            PCategory catetory = categoryManager.GetModel(categoryId, (LanguageEnum)LanguageID);

            if (catetory == null)
                return "";

            return catetory.CategoryName;
        }
        /// <summary>
        /// 在前台通过js弹出消息窗口
        /// </summary>
        /// <param name="msg">弹出消息</param>
        protected void Alert(string msg)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "ok", "<script>alert(\"" + msg + "\");</script>");
        }


        public string CurrentUserName
        {
            get
            {                                                  //  AuthenticationUserTitle     
                return Session["User"] == null ? null : ((UserS)Session["User"]).UserName;
            }
        }

        public UserS CurrentUser
        {
            get
            {
                if (
                    Session["User"] != null)
                {
                    UserS u = Session["User"] as UserS;
                    return u;
                }
                return null;
            }
        }






        /// <summary>
        /// 过滤用户输入,防止sql注入
        /// </summary>
        /// <returns></returns>
        public void checkStrValid(string input)
        {
            if (string.IsNullOrEmpty(input))
                return;


            string[] sql = new string[] { "%", ";", "'", ">", "<", "=", "declare", "select", "insert", "update", "delete", "drop", "create", "exec" };
            bool result = false;
            foreach (string str in sql)
                if (input.Contains(str))
                {
                    result = true;
                    break;
                }

            if (result)
                Response.Redirect("/Error.aspx");
        }


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }



        public UserType CurrentUserType
        {
            get
            {
                if (Session["User"] != null)
                {
                    UserS u = Session["User"] as UserS;
                    return u.UserType;
                }
                return UserType.Viewer;
            }
        }
    }
}